<?php
/*
	
*/
defined('BY_JYA') or exit('Access Denied');
global $_RQ,$_AR;
$_RQ['op']=$_RQ['op']?$_RQ['op']:'main';
$return['ischecklogin']=model('jy_weishop/member')->checklogin('','','','',true);
if($_RQ['op']=='main'){
	$code=$_RQ['code'];
	if (empty($code)) {
		exi('请输入卡片码!','error','jy_kabao/index/');
	}
	if(substr($code,0,2)!='NO'){
		$code='NO'.$code;
	}
	$batch_code=pdo_get('jy_kabao_batch_code',array('uniacid'=>$_SESSION['uniacid'],'sn'=>$code));
	if(empty($batch_code)){
		exi('卡片码错误,无权操作'.$code,'error','jy_kabao/index/');   
	}
	if($batch_code['mid'] && $batch_code['mid']!=$_SESSION['uid']){
		exi('该卡片被别人领取了！','error','jy_kabao/index/');   
	}
	if($batch_code['pluginname']=='jy_zhongyi'){
		$params=array(
			'from_type'=>3,
			'workerid'=>$batch_code['workerid'],
			'storeid'=>$batch_code['storeid'],
		);
		model('jy_zhongyi/model')->checkdangan(0,$params);
	}
	$isfollow=pdo_getcolumn('core_fans',array('uniacid'=>$_SESSION['uniacid'],'mid'=>$_SESSION['uid']),'follow');	
	if(!$isfollow && $return['pluginset']['is_fans']){
		$return['showwechatcode']=1;
	}
	$return['showwechatcode']=1;
	if($_SESSION['uid'] && !$batch_code['status']){
		pdo_update('jy_kabao_batch_code',array('status'=>1,'mid'=>$_SESSION['uid'],'looktime'=>time()),array('uniacid'=>$_SESSION['uniacid'],'sn'=>$code));
	}
	$item=pdo_get('jy_kabao_list',array('uniacid'=>$_SESSION['uniacid'],'id'=>$batch_code['listid']));
	$item['images']=json_decode($item['images'],true);
	$item['set']=json_decode($item['set'],true);
	$batch=pdo_get('jy_kabao_batch',array('uniacid'=>$_SESSION['uniacid'],'id'=>$batch_code['batchid']));
	if($batch['storeid']){
		$store=pdo_get('jy_weishop_store',array('id'=>$batch['storeid'],'uniacid'=>$_SESSION['uniacid']));
	}
	if($batch['wokerid'] && !$batch['pluginname']){
		$worker=pdo_get('jy_weishop_store_worker',array('id'=>$batch['wokerid'],'uniacid'=>$_SESSION['uniacid']));
	}
	$credit_types=credit_types();
	foreach($item['set'] as &$sv){
		if(!$sv['type']){
			$sv['goods']=pdo_get('jy_weishop_goods',array('id'=>$sv['goodsid']),array('title','image','price','subtitle','type'));
		}elseif($sv['type']==1){
			$coupon=model('jy_weishop/coupon')->intcoupon($sv['couponid']);
			$sv['coupon']=array_fields(array('title2','title3','title'),$coupon);
		}elseif($sv['type']==3){
			$sv['credit_title']=$credit_types[$sv['credit_type']];
		}
	}
	$return['post']=array(
		'codeid'=>$batch_code['id'],
	);
	$return['item']=$item;
	$return['store']=$store;
	$return['worker']=$worker;
	$return['batch_code']=$batch_code;
	$return['batch']=$batch;
	set_html('title',$item['title']);
    exi($return);
}

if($_RQ['op']=='post'){
	$post=get_postdata();
	$this->model('order')->add_order($post);
	exi('兑换成功！','',$return['pluginset']['homelink']);
}